 

layui.use('table',function(){
  var table = layui.table;
  var form = layui.form;
 
  
 //监听表格复选框选择
  table.on('checkbox(demo)', function(obj){
	  
       form.render(); //单选复选下拉框渲染
	  var data=obj.data;
	 if(obj.type=="all"){
		
		 
		 $("#checkedDiv").html("")
		 var tables=table.cache.idTest;
		 if(obj.checked==true){
			 	if(billOfMaterialText==""){
			 		 $.each(tables,function(i,item){
			 			 billOfMaterialText+=item.rawstockId+","+muId+","+decodeURI(muName)+","+item.rawstockName+","+0+"#";
			 		});
			 		addAllBillOfMaterial();
			 		showDiv();
				}else{
					var text="";
					
					$.each(tables,function(i,item){
						var count=0;
						var billOfMaterials = new Array()
			            billOfMaterials=billOfMaterialText.split("#");
						 for (var j = 0; j < billOfMaterials.length; j++) {
							
							if(billOfMaterials[j].split(",")[0]==item.rawstockId){
								count=1;
							};
						};
						if(count==0){
							billOfMaterialText+=item.rawstockId+","+muId+","+decodeURI(muName)+","+item.rawstockName+","+0+"#";
						}
					});
					addAllBillOfMaterial();
					showDiv();
					 
				}
		 }else{
			 $.each(tables,function(i,item){
				   deleteAllBillOfMaterial(item)				
			 })
			 showDiv();
		 }
		 
		 
	 }else{
		 if(obj.checked==true){
			 addBillOfMaterial(data);
			 $("#checkedDiv").append("<div id='checkedDiv"+data.rawstockId+"' class='checkedDiv flag note note--success new-item' style='width: 13%;float:left;margin-left:1%;' onclick='removeChecked(\"checkedDiv"+data.rawstockId+"\",\""+data.rawstockId+"\")'>" +
	  					"<div class='flag__image note__icon'><i class='fa fa-check'></i></div>"+
	  					" <div class='flag__body note__text'>"+data.rawstockName+"</div>"+
						"</div>")
		 }else{
			 deleteBillOfMaterial(data);
			 $("#checkedDiv"+data.rawstockId).removeClass("new-item").addClass("remove-item");
			 setTimeout(() => {
				 $("#checkedDiv"+data.rawstockId).remove();
			}, 500);
		 }
	 }
 
  });
 
//表单重载
  var $ = layui.$, active = {
    reload: function(){
    	var rawstockName=$("#rawstockName").val();
      //执行重载    查询
      table.reload('idTest2', {
    	  
        page: {
          curr: 1 //重新从第 1 页开始
        }
      	,where: {
      		rawstockName: rawstockName,
        }
      });
    }
  };
  
  
  
 
  $('#materialSreach').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });
  
 
  
});
 
 

layui.use(['form', 'table'], function(){
	  var form = layui.form;
	  var table = layui.table;
	  var layer = layui.layer;
	  
	 
    	

    	  $("#submitConfigure").on('click', function(){
    		 var booleans=true;
     		 if(billOfMaterialText==""){
     				booleans=false;
     				layer.msg("请选择材料") 
     		 }else{
  				booleans=true;
  			}
     		 
     		if(booleans){
    			var boolean=false;
    			var billOfMaterials = new Array()
    			billOfMaterials=billOfMaterialText.split("#");
			    for (var i = 0; i < billOfMaterials.length; i++) {
			    	if(billOfMaterials[i]){
			    		var number=$("#"+billOfMaterials[i].split(",")[0]).val()
						if(parseInt(number)<1){
							boolean=false;
							break ;
						}else{
							boolean=true;
						}
			    	}
			    	
				}
			    console.info(boolean)
			    if(boolean){
	    			var configure=billOfMaterialText;
	    			var url="../../listenBillOfMaterial/addBom.action";
	    			var data={"configure":billOfMaterialText,"bomIds":bomIds,"muId":muId}
	    			$.post(url,data,function(mes){
	    				if(mes>0){
	    					  window.parent.location.reload()
	    					  var index=parent.layer.getFrameIndex(window.name);
	    					  parent.layer.close(index);
	    				  }
	    			},'json')
			    }else{
			    	layer.msg("请输入材料采购材料数量,不能小于0")
			    }
    			
    		}
    		  
    		  

    	  })
	  
	  
});

function input(obj,event){
	//var che=$(obj).parent().parent().parent().find(".layui-table-col-special input[name='layTableCheckbox']")
	
	
	var keycode = String.fromCharCode(event.which); 
	var first=$(obj).val().substr(0,1);
	var noFs=/^[0-9]*$/;
	 
	var numbers=0;
	var prices=0;
		if($(obj).val()==""){
			$(obj).val(0)
		}else{
			if(!noFs.test($(obj).val())){
				$(obj).val(0)
			}else{
				if(first==0){
					$(obj).val("")
					$(obj).val(keycode)
					
				}
				 
			}
		}
		
		console.info(billOfMaterialText)
	 
		billOfMaterialText="";
		$("#billOfMaterialTbody").find("tr").each(function(i,item){
			var rawIdTd=$(item).children().get(0);
			var rawNameTd=$(item).children().get(3);
			var numberTd=$(item).children().get(4);
			  
			billOfMaterialText+=$(rawIdTd).text()+","+muId+","+decodeURI(muName)+","+$(rawNameTd).text()+","+$(numberTd).children().val()+"#";
		 
		});
		
		 console.info(billOfMaterialText)
}

 


function addAllBillOfMaterial(){
	var billOfMaterials = new Array()
	billOfMaterials=billOfMaterialText.split("#");
	 $("#billOfMaterialTbody").html("")
	  for (var i = 0; i < billOfMaterials.length; i++) {
		  if(billOfMaterials[i]){
			  $("#billOfMaterialTbody").append("<tr>"
						+"<td style='display:none;'>"+billOfMaterials[i].split(",")[0]+"</td>"
						+"<td style='display:none;'>"+billOfMaterials[i].split(",")[1]+"</td>"
						+"<td>"+billOfMaterials[i].split(",")[2]+"</td>"
						+"<td>"+billOfMaterials[i].split(",")[3]+"</td>"
						+"<td style='width:200px;'><input type='text' name='title' id='"+billOfMaterials[i].split(",")[0]+"'  required lay-verify='required' value="+billOfMaterials[i].split(",")[4]+"  class='layui-input' style='border:0px;height:22px;' onkeyup='input(this,event)'></td>"
						+"<td style='text-align:center;'><a class='layui-btn layui-btn-danger layui-btn-xs' lay-event='del' onclick='removeChecked(\"checkedDiv"+billOfMaterials[i].split(",")[0]+"\",\""+billOfMaterials[i].split(",")[0]+"\")'>删除</a></td>"
						+"</tr>")
		  }
		  
	  }
}

function deleteAllBillOfMaterial(item){
	 var billOfMaterials = new Array()
	 billOfMaterials=billOfMaterialText.split("#");
	 billOfMaterialText="";
	 for (var i = 0; i < billOfMaterials.length-1; i++) {
		if(billOfMaterials[i].indexOf(item.rawstockId)!=0){
			billOfMaterialText+=billOfMaterials[i]+"#"
		} 
	}
	 
	 
	
	 $("#billOfMaterialTbody").html("")
	  billOfMaterials=billOfMaterialText.split("#");
	  for (var i = 0; i < billOfMaterials.length; i++) {
		  if(billOfMaterials[i]){
			  $("#billOfMaterialTbody").append("<tr>"
						+"<td style='display:none;'>"+billOfMaterials[i].split(",")[0]+"</td>"
						+"<td style='display:none;'>"+billOfMaterials[i].split(",")[1]+"</td>"
						+"<td>"+billOfMaterials[i].split(",")[2]+"</td>"
						+"<td>"+billOfMaterials[i].split(",")[3]+"</td>"
						+"<td style='width:200px;'><input type='text' name='title'id='"+billOfMaterials[i].split(",")[0]+"'  required lay-verify='required' value="+billOfMaterials[i].split(",")[4]+"  class='layui-input' style='border:0px;height:22px;' onkeyup='input(this,event)'></td>"
						+"<td style='text-align:center;'><a class='layui-btn layui-btn-danger layui-btn-xs' lay-event='del' onclick='removeChecked(\"checkedDiv"+billOfMaterials[i].split(",")[0]+"\",\""+billOfMaterials[i].split(",")[0]+"\")'>删除</a></td>"
						+"</tr>")
		  }
		  
	  }
}

function addBillOfMaterial(data){
	
	var billOfMaterials = new Array()
	 billOfMaterialText+=data.rawstockId+","+muId+","+decodeURI(muName)+","+data.rawstockName+","+0+"#";
	 billOfMaterials=billOfMaterialText.split("#");
	 $("#billOfMaterialTbody").html("")
	  for (var i = 0; i < billOfMaterials.length; i++) {
		  if(billOfMaterials[i]){
			  console.info(billOfMaterials[i].split(",")[0])
			  $("#billOfMaterialTbody").append("<tr>"
						+"<td style='display:none;'>"+billOfMaterials[i].split(",")[0]+"</td>"
						+"<td style='display:none;'>"+billOfMaterials[i].split(",")[1]+"</td>"
						+"<td>"+billOfMaterials[i].split(",")[2]+"</td>"
						+"<td>"+billOfMaterials[i].split(",")[3]+"</td>"
						+"<td style='width:200px;'><input type='text' name='title' id='"+billOfMaterials[i].split(",")[0]+"'  required lay-verify='required' value="+billOfMaterials[i].split(",")[4]+"  class='layui-input' style='border:0px;height:22px;' onkeyup='input(this,event)'></td>"
						+"<td style='text-align:center;'><a class='layui-btn layui-btn-danger layui-btn-xs' lay-event='del' onclick='removeChecked(\"checkedDiv"+billOfMaterials[i].split(",")[0]+"\",\""+billOfMaterials[i].split(",")[0]+"\")'>删除</a></td>"
						+"</tr>")
		  }
		  
	  }
}

function deleteBillOfMaterial(data){
	 var billOfMaterials = new Array()
	 billOfMaterials=billOfMaterialText.split("#");
	 billOfMaterialText="";
	 for (var i = 0; i < billOfMaterials.length-1; i++) {
		if(billOfMaterials[i].indexOf(data.rawstockId)!=0){
			billOfMaterialText+=billOfMaterials[i]+"#"
		} 
	}
	 
	 
	
	 $("#billOfMaterialTbody").html("")
	  billOfMaterials=billOfMaterialText.split("#");
	  for (var i = 0; i < billOfMaterials.length; i++) {
		  if(billOfMaterials[i]){
			  $("#billOfMaterialTbody").append("<tr>"
						+"<td style='display:none;'>"+billOfMaterials[i].split(",")[0]+"</td>"
						+"<td style='display:none;'>"+billOfMaterials[i].split(",")[1]+"</td>"
						+"<td>"+billOfMaterials[i].split(",")[2]+"</td>"
						+"<td>"+billOfMaterials[i].split(",")[3]+"</td>"
						+"<td style='width:200px;'><input type='text' name='title' id='"+billOfMaterials[i].split(",")[0]+"'  required lay-verify='required' value="+billOfMaterials[i].split(",")[4]+"  class='layui-input' style='border:0px;height:22px;' onkeyup='input(this,event)'></td>"
						+"<td style='text-align:center;'><a class='layui-btn layui-btn-danger layui-btn-xs' lay-event='del' onclick='removeChecked(\"checkedDiv"+billOfMaterials[i].split(",")[0]+"\",\""+billOfMaterials[i].split(",")[0]+"\")'>删除</a></td>"
						+"</tr>")
		  }
		  
	  }
}

function showDiv(){
	var billOfMaterials = new Array()
	billOfMaterials=billOfMaterialText.split("#");

	 for (var i = 0; i < billOfMaterials.length-1; i++) {
	       // $("#checkedDiv").append("<div id='checkedDiv"+billOfMaterials[i].split(",")[0]+"' style='width:100px;height:20px;border:1px solid black;float:left' onclick='removeChecked(\"checkedDiv"+billOfMaterials[i].split(",")[0]+"\",\""+billOfMaterials[i].split(",")[0]+"\")'>"+billOfMaterials[i].split(",")[3]+"</div>")
	        
	        $("#checkedDiv").append("<div id='checkedDiv"+billOfMaterials[i].split(",")[0]+"' class='checkedDiv flag note note--success new-item' style='width: 13%;float:left;margin-left:1%;' onclick='removeChecked(\"checkedDiv"+billOfMaterials[i].split(",")[0]+"\",\""+billOfMaterials[i].split(",")[0]+"\")'>" +
	  					"<div class='flag__image note__icon'><i class='fa fa-check'></i></div>"+
	  					" <div class='flag__body note__text'>"+billOfMaterials[i].split(",")[3]+"</div>"+
						"</div>")
	}
	 
	
}

function removeChecked(id,raId){
	$("#"+id).removeClass("new-item").addClass("remove-item");
	 setTimeout(() => {
		 $("#"+id).remove();
	}, 500);
	 var billOfMaterials = new Array()
	 billOfMaterials=billOfMaterialText.split("#");
	 billOfMaterialText="";
	 for (var i = 0; i < billOfMaterials.length-1; i++) {
		if(billOfMaterials[i].indexOf(raId)!=0){
			billOfMaterialText+=billOfMaterials[i]+"#";
		} 
	}
	
	$('#rawstockType').next().find('.layui-table-body tr').each(function(index,item){
		var ids=$(item).find('td[data-field="rawstockId"]').text();
		if(raId==ids){
			var index=$(item).data('index');
            var che = $('#rawstockType').next().find('.layui-table-body tr[data-index="'+index+'"] input[name="layTableCheckbox"]')
            che.prop("checked",false)
           load();
		}
	});
 
	var billOfMaterials = new Array()
	 billOfMaterials=billOfMaterialText.split("#");
	 $("#billOfMaterialTbody").html("")
	  for (var i = 0; i < billOfMaterials.length; i++) {
		  if(billOfMaterials[i]){
			  $("#billOfMaterialTbody").append("<tr>"
						+"<td style='display:none;'>"+billOfMaterials[i].split(",")[0]+"</td>"
						+"<td style='display:none;'>"+billOfMaterials[i].split(",")[1]+"</td>"
						+"<td>"+billOfMaterials[i].split(",")[2]+"</td>"
						+"<td>"+billOfMaterials[i].split(",")[3]+"</td>"
						+"<td style='width:200px;'><input type='text' name='title' id='"+billOfMaterials[i].split(",")[0]+"'  required lay-verify='required' value="+billOfMaterials[i].split(",")[4]+"  class='layui-input' style='border:0px;height:22px;' onkeyup='input(this,event)'></td>"
						+"<td style='text-align:center;'><a class='layui-btn layui-btn-danger layui-btn-xs' lay-event='del' onclick='removeChecked(\"checkedDiv"+billOfMaterials[i].split(",")[0]+"\",\""+billOfMaterials[i].split(",")[0]+"\")'>删除</a></td>"
						+"</tr>")
		  }
		  
	  }
}






  function load(){
  	var form = layui.form;
  	form.render();
  }